package org.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.common.JwtHelper;
import org.example.common.Result;
import org.example.common.ResultCodeEnum;
import org.example.mapper.CategoryMapper;
import org.example.pojo.Admin;
import org.example.pojo.Business;
import org.example.pojo.Category;
import org.example.pojo.Query;
import org.example.service.BusinessService;
import org.example.mapper.BusinessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 13326
* @description 针对表【hards_business】的数据库操作Service实现
* @createDate 2024-12-29 20:48:50
*/
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
    implements BusinessService{
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Autowired
    private JwtHelper jwtHelper;


@Override
    public Result searchlist(Query query){
    LambdaQueryWrapper<Business> queryWrapper=new LambdaQueryWrapper();
    queryWrapper.like(!StringUtils.isEmpty(query.getKeyword()), Business::getUsername,query.getKeyword())
            .eq(Business::getUsername,query.getKeyword());
    IPage<Business> page=new Page<>(query.getPageNum(),query.getPageSize());
    businessMapper.selectPageMap(page,query);
    HashMap<String,Object> map=new HashMap<>();
    map.put("total",page.getTotal());
    map.put("rows",page.getRecords());
    map.put("pages",page.getPages());
    map.put("size",page.getSize());


    return Result.ok(map);

}
@Override
    public Result add(Business business){
    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getUsername,business.getUsername());
    Long count=businessMapper.selectCount(lambdaQueryWrapper);
    if(count>0){
    return     Result.build("该用户已存在",ResultCodeEnum.USERNAME_USED);

    }
   int rows= businessMapper.insert(business);

    return Result.ok("新增成功");


}
@Override
   public Result updatebus(Business business){

    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getUsername,business.getUsername());
    Long count=businessMapper.selectCount(lambdaQueryWrapper);
//    if(count>1){
//        return     Result.build("该用户已存在",ResultCodeEnum.USERNAME_USED);
//
//    }
    int rows= businessMapper.updateById(business);

    return Result.ok("修改成功");
}
@Override
    public Result deleteById(Integer id){
    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getId,id);
    Long count=businessMapper.selectCount(lambdaQueryWrapper);
    if(count==0){
        return  Result.build("该数据不存在",ResultCodeEnum.ID_ERROR);
    }
    businessMapper.deleteById(id);
    return Result.ok("删除成功");
}
@Override
    public Result register(Business business){
    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getUsername,business.getUsername());
    Long count=businessMapper.selectCount(lambdaQueryWrapper);
    if(count>0){
        return  Result.build("该用户已存在",ResultCodeEnum.USERNAME_USED);

    }
    business.setName(business.getUsername());
    business.setRole("BUSINESS");
    int rows= businessMapper.insert(business);

    System.out.println("123");
    return Result.ok("新增成功");

}
@Override
    public Result login(Admin admin){

    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getUsername,admin.getUsername());
    Business business=businessMapper.selectOne(lambdaQueryWrapper);
    if(business == null){
        return  Result.build(501,ResultCodeEnum.USERNAME_ERROR);

    }
    if(!business.getPassword().equals(admin.getPassword())){
        return Result.build(503,ResultCodeEnum.PASSWORD_ERROR);

    }
    Map<String,Object> map=new HashMap<>();
    String token=jwtHelper.createToken(Long.valueOf(business.getId()));

    map.put("userinfo",business);
    map.put("token",token);
    return Result.ok(map);



}
    @Override
    public Result updateperson(Business business){
    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Business::getId,business.getId());
    Business business1=businessMapper.selectOne(lambdaQueryWrapper);
    if(business1 == null){
        return Result.build("501",ResultCodeEnum.USERNAME_ERROR);
    }
    int rows=businessMapper.updateById(business);
    return Result.ok("修改成功");
 }
    @Override
    public Result updatepassword(Business business){
        LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Business::getId,business.getId());
        Business business1=businessMapper.selectOne(lambdaQueryWrapper);
        System.out.println(business1.getUsername());

        int rows=businessMapper.updateById(business);
        System.out.println(rows);
        return Result.ok("修改成功");
    }
    @Override
    public Result getall(){
    LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        List<Business> list=businessMapper.selectList(lambdaQueryWrapper);
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        return Result.ok(map);
    }
    @Override
    public Result getdetail(Integer id){
    Business business=businessMapper.selectById(id);
        System.out.println(business);
        return Result.ok(business);

    }
}




;